package iceberg.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class Layer {
	private static final Logger LOG = LoggerFactory.getLogger(Layer.class);

	private final Set<Layer> allowedToAccess = new HashSet<Layer>();
	private final Set<Unit> containedUnits = new HashSet<Unit>();
	private final String name;
	private final int number;
	private final Functionality functionality;

	public Layer(String name, int number) {
		this(name, number, Functionality.UNDEFINED);
	}

	public Layer(String name, int number, Functionality functionality) {
		if (name == null) {
			throw new IllegalArgumentException("Name can't be null");
		}
		if (functionality == null) {
			throw new IllegalArgumentException("Functionality can't be null");
		}
		this.number = number;
		this.functionality = functionality;
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void addAllowedToAccessLayer(Layer layer) {
		allowedToAccess.add(layer);
	}

	public void addUnit(Unit unit) {
		if (unit == null) {
			throw new IllegalArgumentException();
		}

		containedUnits.add(unit);
	}

	public Functionality getFunctionality() {
		return functionality;
	}

	public String getName() {
		return name;
	}

	public boolean allowedToAccess(Layer location) {
		LOG.info("Allowed to access: {}", allowedToAccess);
		return location == this || allowedToAccess.contains(location);
	}

	@Override
	public String toString() {
		return "[Layer::" + name + "," + number + "]";
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return obj != null && obj.getClass() == getClass() && ((Layer) obj).name.equals(name);
	}

	public boolean contains(Unit classUnit) {
		return containedUnits.contains(classUnit);
	}
}
